/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fftest;

/**
 *
 * @author jokeyo
 */
import flanagan.math.FourierTransform;
import java.beans.*;
import java.io.File;

/**
 *
 * @author jokeyo
 */
public class FFTest 
{
    private String OutputFile;
    private String sampleSizeInBits; 
    private String InputFile;
    private File in;



    public static void main(String[] args) 
    {
        final int W = 1024; // example window size 
        double[] data = new double[0];//W*getOutputFile().getSampleSizeInBits()/8]; // input buffer for reading
        double[] inbuf = new double[W]; // input for FFT
        double[] fftbuf = new double[W/2]; // output for FFT (half the window size due to +/- frequency aliasing) 

    // Get the number of audio channels in the wav file
        try
        {
            File in = new File("F:\\Desktop\\200hz.wav");
            WavFile wavFile = WavFile.openWavFile(in);

            int numChannels = wavFile.getNumChannels();
            //int numChannels = 1; // <-- Assuming an audio file will always be MONO

            // Create a buffer of 1024 frames
            double[] buffer = new double[1024 * numChannels];

            int framesRead = 0;
            double min = Double.MAX_VALUE;
            double max = Double.MIN_VALUE;

            do
            {
                // Read frames into buffer
                framesRead = wavFile.readFrames(buffer, 1024);//Grant's machine doesn't work with 1024

                // Loop through frames and look for minimum and maximum value
                for (int s=0 ; s<framesRead * numChannels ; s++)
                {
                    if (buffer[s] > max) {max = buffer[s];}
                    if (buffer[s] < min) {min = buffer[s];}                        
                }                    
            }
            while (framesRead != 0);

      
            /* Read an entire buffer's worth */
            /*while(true) 
            {
                int numdoublesRead = 0;
                while(numdoublesRead < data.length)
                {
                    int tmp = inFile.readFrames(data,data.length-numdoublesRead);
                    if(tmp < 0) 
                    { 
                        // EOS
                        break;
                    }
                    numdoublesRead += tmp;
                }
                //Here, data contains the right number of PCM samples Decode data into inbuf's double[] samples
                decode(data,inbuf);
            }*/

            assert buffer.length == 2*inbuf.length;   

            for(int i=0; i < inbuf.length; i++) 
            {   
                //inbuf[i] = (short)(((0xFF & buffer[2*i+1]) << 8) | (0xFF & buffer[2*i]));  
                //inbuf[i] /= Short.MAX_VALUE; // normalise to [-1,1]   
                //System.out.println(buffer[i]);
            }
        }
        catch (Exception e)
        {
            System.err.println(e);
        }
    }
}